/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import MODEL.Categoria;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author MatheuseJessica
 */
public class DaoCategoria extends Banco{
    
    public DaoCategoria(){
        connection = Conecta();
    }
    
    public void insereCategoria(Categoria cat){
        try{
            String SQL = "INSERT INTO categoria VALUES (default,?)";
            PreparedStatement stmt = connection.prepareStatement(SQL);
            stmt.setString(1, cat.getDescricao());
            stmt.execute();
            stmt.close();
        }catch(Exception e){
            
        }finally{
            Fecha();
        }
       
    }
    
    public List<Categoria> getCategoria(){
        List<Categoria> categoria = new ArrayList<Categoria>();
        try{
            Statement stm = connection.createStatement();
             // Vamos executar o seguinte comando SQL :  
            String SQL = "SELECT * FROM categoria";  
            ResultSet rs = stm.executeQuery(SQL);
            
            while(rs.next()){
                Categoria cat = new Categoria();
                cat.setCodigo(rs.getInt("cod_categoria"));
                cat.setDescricao(rs.getString("descricao"));
                categoria.add(cat);
            }
            
        }catch(Exception e){
            
        }finally{
            Fecha();
        }

        return categoria;
       
    }
    
    public Categoria getCategoria (int categoria){
        try{
            Statement stm = connection.createStatement();
            String SQL = "SELECT * FROM categoria WHERE cod_categoria="+categoria;
            ResultSet rs = stm.executeQuery(SQL);
            while(rs.next()){
                return new Categoria(categoria, rs.getString("descricao"));
            }
        }catch(Exception e){
            System.out.println("Erro getCategoria(int categoria): "+e);
        }finally{
            Fecha();
        }
        return null;
    }
}
